package com.infinitegroup.findmystuff.presenters;

import java.util.List;
import java.util.Map;

import android.content.Context;

import com.infinitegroup.findmystuff.models.Account;
import com.infinitegroup.findmystuff.models.AccountLink;
import com.infinitegroup.findmystuff.views.HomeView;


public class HomePresenter{
	
	private Account account; // this is the account passed in by the login screen.
	private HomeView view;
	
	public HomePresenter(HomeView view, String username){
		this.view = view;
		account = new AccountLink(username, (Context) view); // establish a new account link with the username passed in.
															// this also populates all account fields, as a username is passed in.
	}
	
	
	/**
	 * Gets items associated with the username and returns them as a list of maps.
	 * @return List of items (maps).
	 */
	public List<Map<String,String>> getItemsToDisplay() {
		try{
			account.refresh(account.getUsername());
			List<Map<String,String>> items = account.getAccountItems();
			if (items.isEmpty()){
				return null;
			} else {
				return items;
			}
		} catch (Exception e){
			view.displayMessage("Could not fetch items from database");
			return null;
		}
	}
	
	public void updateWelcomeMessage(){
		view.updateWelcome(account.getUsername());
	}
	
	public void handleAddItemClick(){
		view.advanceToAddItemView(account.getUsername());
	}
	
	public void handleDeleteItemsClick(){
		
	}
	
	public void handleLogoutClick(){
		view.advanceToLogin();
		view.end();
	}

	public String getFirstName() {
		return account.getFirstName();
	}


	public void handleSeeAllItemsClick() {
		view.advanceToViewAllView();	
	}

	
	

}
